package com.abc.xxx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abc.xxx.dao.AccessibleDeptMapper;
import com.abc.xxx.model.AccessibleDept;
import com.abc.xxx.service.AccessibleDeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AccessibleDeptServiceImpl extends ServiceImpl<AccessibleDeptMapper, AccessibleDept> implements AccessibleDeptService {

    @Override
    public List<Integer> findDeptIdsByRoleId(Integer userRoleId) {
        QueryWrapper<AccessibleDept> w = new QueryWrapper<>();
        w.select("dept_id");
        w.eq("role_id",userRoleId);
        return list(w).stream().map(AccessibleDept::getDeptId).collect(Collectors.toList());
    }

    @Transactional
    @Override
    public void assignCustomDataScopePermissions(Integer roleId, List<AccessibleDept> accessibleDepts) {
        log.info("step_1: 删除角色原来关联的自定义可访问的部门");
        QueryWrapper<AccessibleDept> dw = new QueryWrapper<>();
        dw.eq("role_id",roleId);
        remove(dw);

        log.info("step_2: 重新添加角色关联的自定义可访问的部门");
        saveBatch(accessibleDepts);
    }


}
